﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region RPTScrapConcreteDao

        public List<tb_RPT_ScrapConcrete> GetAllScrapConcrete()
        {

            var query = (from obj in this.tb_RPT_ScrapConcretes
                         where obj.IsDelete == 0
                         select obj).ToList();
            return query;
        }
        public List<tb_RPT_ScrapConcrete> GetScrapConcreteByInputDate(DateTime inputdate)
        {

            var query = (from obj in this.tb_RPT_ScrapConcretes
                         where obj.IsDelete == 0 && obj.InputDate==inputdate.ToShortDateString ()
                         select obj).ToList();
            return query;
        }
        public List<tb_RPT_ScrapConcrete> GetScrapConcreteByInputDate(DateTime inputdate1, DateTime inputdate2)
        { 
            var query = (from obj in this.tb_RPT_ScrapConcretes
                         where obj.IsDelete == 0 && DateTime.Parse(obj.InputDate) >= inputdate1 && DateTime.Parse(obj.InputDate)  <= inputdate2
                         select obj).ToList();
            return query;
        }

        public List<tb_RPT_ScrapConcrete> GetScrapConcreteByContition(string strWeather, string strProjectName, string strInputDate
                                                                    , string strConstructionUnit, string strConstructionSite
                                                                    , string strIntensityLevel, string strDevelopmentTime
                                                                    , string strQuantity, string strCement, string strProductLine
                                                                    , string strApproachTo, string strCarryOn)
        {
            var tq = from obj in this.tb_RPT_ScrapConcretes
                     where obj.IsDelete == 0
                     select obj;

            if (strWeather.Length > 0)
            {
                tq = tq.Where(entity => entity.Weather == strWeather); 
            }

            if (strProjectName.Length > 0)
            {
                tq = tq.Where(entity => entity.ProjectName == strProjectName);
            }
            if (strInputDate.Length > 0)
            {
                tq = tq.Where(entity => entity.InputDate == strInputDate);
            }
            if (strConstructionUnit.Length > 0)
            {
                tq = tq.Where(entity => entity.ConstructionUnit == strConstructionUnit);
            }
            if (strConstructionSite.Length > 0)
            {
                tq = tq.Where(entity => entity.ConstructionSite == strConstructionSite);
            }
            if (strIntensityLevel.Length > 0)
            {
                tq = tq.Where(entity => entity.IntensityLevel == strIntensityLevel);
            }
            if (strDevelopmentTime.Length > 0)
            {
                tq = tq.Where(entity => entity.DevelopmentTime == strDevelopmentTime);
            }
            if (strQuantity.Length > 0)
            {
                tq = tq.Where(entity => entity.Quantity == strQuantity);
            }
            if (strCement.Length > 0)
            {
                tq = tq.Where(entity => entity.Cement == strCement);
            }
            if (strProductLine.Length > 0)
            {
                tq = tq.Where(entity => entity.ProductLine == strProductLine);
            }
            if (strApproachTo.Length > 0)
            {
                tq = tq.Where(entity => entity.ApproachTo == strApproachTo);
            }
            if (strCarryOn.Length > 0)
            {
                tq = tq.Where(entity => entity.CarryOn == strCarryOn);
            } 

            var query = tq.ToList(); 
            return query;
        }

        public List<tb_RPT_ScrapConcrete> GetScrapConcreteByProjectName(string strProjectName)
        {

            var query = (from obj in this.tb_RPT_ScrapConcretes
                         where obj.IsDelete == 0 && obj.ProjectName == strProjectName
                         select obj).ToList();
            return query;
        } 

        public tb_RPT_ScrapConcrete GetScrapConcreteById(int id)
        {
            return this.tb_RPT_ScrapConcretes.SingleOrDefault(tb_RPT_ScrapConcretes => tb_RPT_ScrapConcretes.Id == id);
        }

        public void CreateScrapConcrete(tb_RPT_ScrapConcrete entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_RPT_ScrapConcretes.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifyScrapConcrete(tb_RPT_ScrapConcrete entity)
        {
            tb_RPT_ScrapConcrete instance = this.tb_RPT_ScrapConcretes.SingleOrDefault(p => p.Id == entity.Id);
            instance.Weather = entity.Weather;
            instance.InputDate = entity.InputDate;
            instance.ProjectName = entity.ProjectName;
            instance.ConstructionUnit = entity.ConstructionUnit;
            instance.ConstructionSite = entity.ConstructionSite;
            instance.IntensityLevel = entity.IntensityLevel;
            instance.DevelopmentTime = entity.DevelopmentTime;
            instance.Reason = entity.Reason;
            instance.Quantity = entity.Quantity;
            instance.Cement = entity.Cement;
            instance.ProductLine = entity.ProductLine;
            instance.ApproachTo = entity.ApproachTo;
            instance.CarryOn = entity.CarryOn; 

            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now; 

            this.SubmitChanges();
        }

        public void DeleteScrapConcrete(tb_RPT_ScrapConcrete entity)
        {
            tb_RPT_ScrapConcrete deleteEntity = tb_RPT_ScrapConcretes.SingleOrDefault(p => p.Id == entity.Id);
            tb_RPT_ScrapConcretes.DeleteOnSubmit(deleteEntity);

            this.SubmitChanges();
        }
         
        #endregion

    }

}
